package pl.edu.agh.gp.nodes.stock;

import org.apache.commons.math.stat.descriptive.DescriptiveStatistics;
import org.jgap.InvalidConfigurationException;
import org.jgap.gp.impl.GPConfiguration;
import pl.edu.agh.gp.nodes.AbstractNode;
import pl.edu.agh.gp.nodes.math.Mean;
import pl.edu.agh.model.core.Price;
import pl.edu.agh.utils.EMA;

import java.util.LinkedList;
import java.util.List;

/**
 * > 70 overbought
 * < 30 oversold
 */
public class RSI extends AbstractNode {
    public RSI(final GPConfiguration a_conf, int initialArity, int maxArity, Class childType) throws InvalidConfigurationException {
        super(a_conf, initialArity, maxArity, "RSI", childType);
    }

    protected double performMathCalculations(DescriptiveStatistics stats) {
        double[] values = stats.getValues();

        double[] ups = new double[values.length - 1];
        double[] downs = new double[values.length - 1];

        double backValue = values[0];
        for(int i = 1; i < values.length; i++){
            double diff = values[i] - backValue;
            if(diff > 0.0){
                ups[i-1] = diff;
                downs[i-1] = 0.0;
            }else{
                ups[i-1] = 0.0;
                downs[i-1] = -diff;
            }
        }

        EMA ema = new EMA();
        double emaUp = ema.getEMA(ups);
        double emaDown = ema.getEMA(downs);

        double RS = emaUp / emaDown;
        double RSI = 100.0 - 100.0/(1.0 + RS);

        return RSI;
    }

    protected Object localClone() throws InvalidConfigurationException {
        return new Mean(getGPConfiguration(), getArity(null), getArityMax(), getChildType(null, 0));
    }
}
